package com.app.signing.view.login

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.app.signing.R
import com.app.signing.app.ADMIN_ID
import com.app.signing.app.ADMIN_PWD
import com.app.signing.data.DbManager
import com.app.signing.data.SpManager
import com.app.signing.view.base.BaseActivity
import com.app.signing.view.main.MainActivity
import kotlinx.android.synthetic.main.activity_login.bt_login
import kotlinx.android.synthetic.main.activity_login.edit_user_faculty
import kotlinx.android.synthetic.main.activity_login.edit_user_id
import kotlinx.android.synthetic.main.activity_login.edit_user_name
import kotlinx.android.synthetic.main.activity_login.edit_user_pwd
import kotlinx.android.synthetic.main.activity_login.edit_user_team
import kotlinx.android.synthetic.main.activity_login.text_admin_login
import kotlinx.android.synthetic.main.activity_login.text_register
import kotlinx.android.synthetic.main.activity_login.toolbar_login

class LoginActivity : BaseActivity() {

    @LoginModel
    private var pageLogin = LoginModel.loginUser

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        if (!TextUtils.isEmpty(SpManager.INSTANCE.readLoginUserId())) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        switchPage()
        text_register.setOnClickListener {
            pageLogin = when (pageLogin) {
                LoginModel.loginAdmin -> {
                    LoginModel.loginUser
                }
                LoginModel.loginUser -> {
                    LoginModel.registerUser
                }
                LoginModel.registerUser -> {
                    LoginModel.loginUser
                }
                else -> {
                    pageLogin
                }
            }
            switchPage()
        }
        text_admin_login.setOnClickListener {
            pageLogin = LoginModel.loginAdmin
            switchPage()
        }
        bt_login.setOnClickListener {
            when (pageLogin) {
                LoginModel.loginAdmin -> {
                    loginAdmin()
                }
                LoginModel.loginUser -> {
                    loginUser()
                }
                LoginModel.registerUser -> {
                    registerUser()
                }
            }
        }
    }

    /**
     * 管理员登录
     */
    private fun loginAdmin() {
        val userId = edit_user_id.text.toString().trim()
        val userPwd = edit_user_pwd.text.toString().trim()
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(userPwd)) {
            showToast(getString(R.string.account_login_no_complete))
            return
        }
        if (ADMIN_ID == userId && ADMIN_PWD == userPwd) {
            SpManager.INSTANCE.saveLoginStates(LoginStates.loginAdmin)
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        } else {
            showToast("您不是管理员，请重新登陆")
        }
    }

    /**
     * 注册用户
     */
    private fun registerUser() {
        val userId = edit_user_id.text.toString().trim()
        val userPwd = edit_user_pwd.text.toString().trim()
        val editName = edit_user_name.text.toString().trim()
        val editFaculty = edit_user_faculty.text.toString().trim()
        val editTeam = edit_user_team.text.toString().trim()
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(userPwd) || TextUtils.isEmpty(
                        editName
                ) || TextUtils.isEmpty(editFaculty) || TextUtils.isEmpty(editTeam)) {
            showToast(getString(R.string.account_register_no_complete))
            return
        }
        // 数据库写操作
        DbManager.INSTANCE.registerUser(userId, editName, editFaculty, editTeam, userPwd)
        pageLogin = LoginModel.loginUser
        switchPage()
    }

    /**
     * 用户登录
     */
    private fun loginUser() {
        val userId = edit_user_id.text.toString().trim()
        val userPwd = edit_user_pwd.text.toString().trim()
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(userPwd)) {
            showToast(getString(R.string.account_login_no_complete))
            return
        }
        val userData = DbManager.INSTANCE.findUser(userId)
        if (userData == null) {
            showToast(getString(R.string.account_no_register))
        } else {
            if (userData.password == userPwd) {
                SpManager.INSTANCE.saveLoginUserId(userId)
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            } else {
                showToast("密码不正确")
            }
        }
    }

    /**
     * 登录页面切换
     */
    private fun switchPage() {
        toolbar_login.title = when (pageLogin) {
            LoginModel.loginAdmin -> {
                getString(R.string.admin_login)
            }
            LoginModel.loginUser -> {
                getString(R.string.user_login)
            }
            LoginModel.registerUser -> {
                getString(R.string.user_register)
            }
            else -> {
                ""
            }
        }
        edit_user_id.text.clear()
        edit_user_pwd.text.clear()
        edit_user_name.visibility = if (pageLogin != LoginModel.registerUser) View.GONE else View.VISIBLE
        edit_user_name.text.clear()
        edit_user_faculty.visibility = if (pageLogin != LoginModel.registerUser) View.GONE else View.VISIBLE
        edit_user_faculty.text.clear()
        edit_user_team.visibility = if (pageLogin != LoginModel.registerUser) View.GONE else View.VISIBLE
        edit_user_team.text.clear()
        bt_login.text = if (pageLogin != LoginModel.registerUser) getString(
                R.string.login
        ) else getString(R.string.register)
        text_register.text = if (pageLogin == LoginModel.loginUser) getString(
                R.string.user_register
        ) else getString(
                R.string.user_login
        )
        text_admin_login.visibility = if (pageLogin == LoginModel.loginUser) View.VISIBLE else View.GONE
    }

}
